<template>
	<view class="form-container">
		<uni-forms-item 
			v-if="['input','textarea','password',].includes(fieldConfig?.type)"
			v-show="fieldConfig.name!='id'"
			:label="fieldConfig?.label"
			:name="fieldConfig?.name" 
			:required="!!fieldConfig?.required"
			:rules="[{ required: !!fieldConfig?.required, errorMessage: fieldConfig?.placeholder || `请输入${fieldConfig?.label}` }]">
			<uni-easyinput
				:type="fieldConfig?.type"
				v-model="formData[fieldConfig?.name]"
				:placeholder="fieldConfig?.placeholder"
				trim="both"
				v-bind="$attrs"/>
		</uni-forms-item>
		<uni-forms-item 
			v-if="['number'].includes(fieldConfig?.type)"
			:label="fieldConfig?.label"
			:name="fieldConfig?.name" 
			:required="!!fieldConfig?.required"
			:rules="[{ required: !!fieldConfig?.required, errorMessage: fieldConfig?.placeholder || `请输入${fieldConfig?.label}` }]">
			<uni-number-box
				v-model="formData[fieldConfig?.name]"
				v-bind="$attrs"/>
		</uni-forms-item>
		<uni-forms-item 
			v-if="['radio','checkbox',].includes(fieldConfig?.type)"
			:label="fieldConfig?.label"
			:name="fieldConfig?.name" 
			:required="!!fieldConfig?.required"
			:rules="[{ required: !!fieldConfig?.required, errorMessage: fieldConfig?.placeholder || `请选择${fieldConfig?.label}` }]">
			<uni-data-checkbox
				v-model="formData[fieldConfig?.name]"
				:multiple="'checkbox' === fieldConfig?.type"
				:map="{text:'text',value:'value'}"
				:localdata="fieldConfig?.dictItemDatas"
				v-bind="$attrs"/>
		</uni-forms-item>
		<uni-forms-item
		 v-if="['dict-select',].includes(fieldConfig?.type)"
		  :label="fieldConfig.label">
			<uni-data-select v-model="formData[fieldConfig?.name]" :localdata="fieldConfig?.dictItemDatas" :map="{text:'text',value:'value'}" :clear="true"  />
		</uni-forms-item>
		<uni-forms-item
			v-if="['list-select',].includes(fieldConfig?.type)"
			:label="fieldConfig?.label"
			:name="fieldConfig?.name" 
			:required="!!fieldConfig?.required"
			:rules="[{ required: !!fieldConfig?.required, errorMessage: fieldConfig?.placeholder || `请选择${fieldConfig?.label}` }]">
			<snowy-sel-picker
				v-model="formData[fieldConfig?.name]"
				:placeholder="fieldConfig?.placeholder"
				:isMultiple = "!!fieldConfig?.isMultiple"
				:map="'API' === fieldConfig?.selectOptionType?{
					label: 'name',
					key: 'id'
				}:{
					label: 'text',
					key: 'value'
				}"
				:dictCode="fieldConfig?.dictTypeCode"
				v-bind="$attrs"/>
		</uni-forms-item>	
		<uni-forms-item
			v-if="['date',].includes(fieldConfig?.type)"
			:label="fieldConfig?.label"
			:name="fieldConfig?.name" 
			:required="!!fieldConfig?.required"
			:rules="[{ required: !!fieldConfig?.required, errorMessage: fieldConfig?.placeholder || `请选择${fieldConfig?.label}` }]">
			<uni-datetime-picker
				:disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.disabled) === 'boolean' ? fieldConfig.disabled : false"
				v-model="formData[fieldConfig.name]"
				:type="fieldConfig?.type"
				:placeholder="fieldConfig.placeholder"
				 return-type="string">
			</uni-datetime-picker>
		</uni-forms-item>
		<uni-forms-item
			v-if="fieldConfig && fieldConfig.type && ['time',].includes(fieldConfig.type) && (typeof(fieldConfig.hidden) === 'boolean' ? !fieldConfig.hidden : true)"
			:label="fieldConfig.label"
			:name="fieldConfig.name" 
			:required="fieldConfig.required" 
			:rules="fieldConfig.rules">
			<view style="flex: 1;">
				<!-- start="09:01" end="21:01" -->
				<picker 
					mode="time"
					:value="formData[fieldConfig.name]" 
					@change="(e)=>{formData[fieldConfig.name] = e.detail.value}">
					<view class="uni-input input-value-border">{{formData[fieldConfig.name]?formData[fieldConfig.name]:fieldConfig.placeholder}}</view>
				</picker>
			</view>
		</uni-forms-item>
		<uni-forms-item
			v-if="fieldConfig && fieldConfig.type && ['switch',].includes(fieldConfig.type) && (typeof(fieldConfig.hidden) === 'boolean' ? !fieldConfig.hidden : true)"
			:label="fieldConfig.label"
			:name="fieldConfig.name" 
			:required="fieldConfig.required" 
			:rules="fieldConfig.rules">
			<switch
				:disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.disabled) === 'boolean' ? fieldConfig.disabled : false"
				:checked = "formData[fieldConfig.name]"
				@change="(e)=>{formData[fieldConfig.name] = e.detail.value}" />
		</uni-forms-item>
		<uni-forms-item
			v-if="fieldConfig && fieldConfig.type && ['uploadFile',].includes(fieldConfig.type) && (typeof(fieldConfig.hidden) === 'boolean' ? !fieldConfig.hidden : true)"
			:label="fieldConfig.label"
			:name="fieldConfig.name" 
			:required="fieldConfig.required" 
			:rules="fieldConfig.rules">
			<snowy-file-picker-api 
				:disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.disabled) === 'boolean' ? fieldConfig.disabled : false" 
				idDataType="objArr"
				v-model="formData[fieldConfig.name]">
			</snowy-file-picker-api>
		</uni-forms-item>
		<uni-forms-item
			v-if="fieldConfig && fieldConfig.type && ['userSelector',].includes(fieldConfig.type) && (typeof(fieldConfig.hidden) === 'boolean' ? !fieldConfig.hidden : true)"
			:label="fieldConfig.label"
			:name="fieldConfig.name" 
			:required="fieldConfig.required" 
			:rules="fieldConfig.rules">
			<snowy-user-picker
				:disabled="typeof(fieldConfigDisable) === 'boolean' ? fieldConfigDisable : typeof(fieldConfig.disabled) === 'boolean' ? fieldConfig.disabled : false"
				v-model="formData[fieldConfig.name]"
				:isMultiple = "fieldConfig.isMultiple"
				:placeholder="fieldConfig.placeholder"
				:map="{key: 'id', label: 'name'}"
				@getOptData="selectorFunction.user.getOptData"
				:isPage="true"
				@getSelData="selectorFunction.user.getSelData">
			</snowy-user-picker>
		</uni-forms-item>
		<uni-forms-item
			v-if="['pageSelect',].includes(fieldConfig?.type)"
			:label="fieldConfig?.label"
			:name="fieldConfig?.name" 
			:required="!!fieldConfig?.required"
			:rules="[{ required: !!fieldConfig?.required, errorMessage: fieldConfig?.placeholder || `请选择${fieldConfig?.label}` }]">
			<snowy-sel-picker
				v-model="formData[fieldConfig?.name]"
				:placeholder="fieldConfig?.placeholder"
				:isMultiple = "false"
				:map="{
					label: 'name',
					key: 'id'
				}"
				:isPage = "true" 
				@getOptData="getOptData"
				@getSelData="getSelData"
				v-bind="$attrs"/>
		</uni-forms-item>
		<!-- <uni-forms-item
			v-if="['selectCustomer',].includes(fieldConfig?.type)"
			:label="fieldConfig?.label"
			:name="fieldConfig?.name" 
			:required="!!fieldConfig?.required"
			:rules="[{ required: !!fieldConfig?.required, errorMessage: fieldConfig?.placeholder || `请选择${fieldConfig?.label}` }]">
			<snowy-tree-picker 
				v-model="formData[fieldConfig?.name]" 
				:placeholder="fieldConfig?.placeholder" 
				@getTreeOptData="getCustomerTreeOpt"
				:dictCode="fieldConfig?.dictTypeCode"
				:map="{key: 'id', parentKey: 'parentId', children: 'children', label: 'name'}"
				 v-bind="$attrs" />
		</uni-forms-item> -->
		<uni-forms-item
			v-if="['selectSupplier',].includes(fieldConfig?.type)"
			:label="fieldConfig?.label"
			:name="fieldConfig?.name" 
			:required="!!fieldConfig?.required"
			:rules="[{ required: !!fieldConfig?.required, errorMessage: fieldConfig?.placeholder || `请选择${fieldConfig?.label}` }]">
			<snowy-tree-picker 
				v-model="formData[fieldConfig?.name]" 
				:placeholder="fieldConfig?.placeholder" 
				@getTreeOptData="getSupplierTreeOpt"
				:dictCode="fieldConfig?.dictTypeCode"
				:map="{key: 'id', parentKey: 'parentId', children: 'children', label: 'name'}"
				 v-bind="$attrs" />
			
		</uni-forms-item>
	</view>
</template>
<script setup>
	import request from '@/utils/request'
	import { ref, reactive,onMounted } from "vue"
	import CallbackState from "@/enum/callback-state"
	const props = defineProps({
		fieldConfig: {
			type: Object,
			required: true
		},
		formData: {
			type: Object,
			required: true
		},
		fieldConfigDisable:{
			type: Boolean,
			default: null,
			required:false
		},
	})
	props.formData[props.fieldConfig.name] = props.fieldConfig.defalutValue
	//const options = uni.$snowy.tool.dictList(props.fieldConfig?.dictTypeCode)
	
	const options = async (param, callback) => {
		if(['radio','checkbox',].includes(props.fieldConfig?.type)){
			const data = await uni.$snowy.tool.dictList(props.fieldConfig?.dictTypeCode)
			return data;
		}
	}
	const getOptData = async (param, callback) => {
		if(['select',].includes(props.fieldConfig?.type)){
			const data = await uni.$snowy.tool.dictList(props.fieldConfig?.dictTypeCode)
			return callback({ state: CallbackState.SUCCESS, data: data })
		}else if(['pageSelect',].includes(props.fieldConfig?.type)){
			const parameter = {
				current: param.pageNo,
				size: param.pageSize
			}
			request({
				url: props.fieldConfig?.selOptionApiUrl,
				method: 'GET',
				data: parameter
			}).then(data => {
				return callback({ state: CallbackState.SUCCESS, data: data?.records })
			})
		}
	}
	
	const getCustomerTreeOpt = (param, callback) => bizPositionApi.positionOrgTreeSelector(param).then((data) => callback({
		state: CallbackState.SUCCESS,
		treeData: data
	}))
	
	const getSupplierTreeOpt = (param, callback) => bizPositionApi.positionOrgTreeSelector(param).then((data) => callback({
		state: CallbackState.SUCCESS,
		treeData: data
	}))
	
	// 根据已选择的数据查询数据
	const getSelData = (curSelDataKey, callback) => {
		if (uni.$snowy.tool.isNotEmpty(curSelDataKey)) {
			request({
				url: props.fieldConfig?.selDataApiUrl,
				method: 'POST',
				data: {
					idList: [curSelDataKey]
				}
			}).then(data => {
				callback(data[0])
			})
		}
	}
	
	// 传递用户选择器需要的API--->复制自snowy-form-item.vue
	const selectorFunction = {
		// 用户
		user: {
			getOptData: async ({ pageNo, pageSize, keyword }, callback) => {
				const parameter = { current: pageNo, size: pageSize, searchKey: keyword }
				const data = await processApi.processUserSelector(parameter)
				callback({ state: CallbackState.SUCCESS, data: data?.records })
			},
			getSelData: async (curSelDataKey, callback) => {
				if (uni.$snowy.tool.isNotEmpty(curSelDataKey)) {
					if(!props.fieldConfig.isMultiple){
						const data = await userCenterApi.userCenterGetUserListByIdList({
							idList: [curSelDataKey]
						})
						callback({ state: CallbackState.SUCCESS, data: data[0] })
						return
					}
					if(!!props.fieldConfig.isMultiple){
						const data = await userCenterApi.userCenterGetUserListByIdList({
							idList: curSelDataKey
						})
						callback({ state: CallbackState.SUCCESS, data: data })
						return
					}
					
				}
			}
		},
	}
</script>

<style scoped>
	.form-container {
	  padding: 20px;
	}
</style>